package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.outside.other.query.SupplierOutsideOrderOtherDTO;
import com.ctshk.rpc.booking.dto.outside.other.query.SupplierOutsideOrderOtherTouristDTO;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderOther;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderOtherTourist;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderOtherMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderOtherTouristMapper;
import com.ctshk.rpc.booking.req.outside.other.save.SupplierOutsideOrderOtherReq;
import com.ctshk.rpc.booking.service.ISupplierOutsideOrderOtherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 站外综费预订信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierOutsideOrderOtherServiceImpl implements ISupplierOutsideOrderOtherService {
    @Autowired
    SupplierOutsideOrderOtherMapper supplierOutsideOrderOtherMapper;
    @Autowired
    SupplierOutsideOrderOtherTouristMapper supplierOutsideOrderOtherTouristMapper;

    @Override
    public void save(Long supplierOrderId, Long userId, LocalDateTime nowDateTime, List<SupplierOutsideOrderOtherReq> supplierOutsideOrderOtherReqList) {
        if (supplierOrderId != null) {
            //删除
            QueryWrapper<SupplierOutsideOrderOther> outsideOrderOtherWrapper = new QueryWrapper<>();
            outsideOrderOtherWrapper.lambda().eq(SupplierOutsideOrderOther::getOrderId, supplierOrderId);
            supplierOutsideOrderOtherMapper.delete(outsideOrderOtherWrapper);
            QueryWrapper<SupplierOutsideOrderOtherTourist> outsideOrderOtherTouristWrapper = new QueryWrapper<>();
            outsideOrderOtherTouristWrapper.lambda().eq(SupplierOutsideOrderOtherTourist::getOtherId, supplierOrderId);
            supplierOutsideOrderOtherTouristMapper.delete(outsideOrderOtherTouristWrapper);
        }
        //新增综费信息
        supplierOutsideOrderOtherReqList.forEach(outsideOrderOtherReq -> {
            SupplierOutsideOrderOther supplierOutsideOrderOther = EntityUtil.copy(outsideOrderOtherReq, SupplierOutsideOrderOther.class);
            supplierOutsideOrderOther.setId(SnowflakeIdWorker.nextId());
            supplierOutsideOrderOther.setOrderId(supplierOrderId);
            supplierOutsideOrderOther.setCreateId(userId);
            supplierOutsideOrderOther.setGmtCreate(nowDateTime);
            supplierOutsideOrderOther.setModifiedId(userId);
            supplierOutsideOrderOther.setGmtModified(nowDateTime);
            supplierOutsideOrderOther.setIsDeleted(IsDeletedCode.NO.getCode());
            supplierOutsideOrderOtherMapper.insert(supplierOutsideOrderOther);
            if (CollectionUtils.isNotEmpty(outsideOrderOtherReq.getSupplierOutsideOrderOtherTouristReqList())) {
                //新增综费旅客信息
                outsideOrderOtherReq.getSupplierOutsideOrderOtherTouristReqList().forEach(outsideOrderOtherTouristReq -> {
                    SupplierOutsideOrderOtherTourist supplierOutsideOrderOtherTourist = EntityUtil.copy(outsideOrderOtherTouristReq, SupplierOutsideOrderOtherTourist.class);
                    supplierOutsideOrderOtherTourist.setId(SnowflakeIdWorker.nextId());
                    supplierOutsideOrderOtherTourist.setOrderId(supplierOrderId);
                    supplierOutsideOrderOtherTourist.setOtherId(supplierOutsideOrderOther.getId());
                    supplierOutsideOrderOtherTourist.setCreateId(userId);
                    supplierOutsideOrderOtherTourist.setGmtCreate(nowDateTime);
                    supplierOutsideOrderOtherTourist.setModifiedId(userId);
                    supplierOutsideOrderOtherTourist.setGmtModified(nowDateTime);
                    supplierOutsideOrderOtherTourist.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierOutsideOrderOtherTouristMapper.insert(supplierOutsideOrderOtherTourist);
                });
            }
        });
    }

    @Override
    public List<SupplierOutsideOrderOtherDTO> selectByOrderId(Long id) {
        List<SupplierOutsideOrderOtherDTO> supplierOutsideOrderOtherDTOS = new ArrayList<>();
        //查询机票列表
        QueryWrapper<SupplierOutsideOrderOther> otherWrapper = new QueryWrapper();
        otherWrapper.lambda().eq(SupplierOutsideOrderOther::getOrderId, id);
        List<SupplierOutsideOrderOther> supplierOutsideOrderOthers = supplierOutsideOrderOtherMapper.selectList(otherWrapper);
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderOthers)) {
            supplierOutsideOrderOtherDTOS = EntityUtil.copyList(supplierOutsideOrderOthers, SupplierOutsideOrderOtherDTO.class);
            supplierOutsideOrderOtherDTOS.forEach(other -> {
                //查询综费旅客列表
                QueryWrapper<SupplierOutsideOrderOtherTourist> otherTouristWrapper = new QueryWrapper();
                otherTouristWrapper.lambda().eq(SupplierOutsideOrderOtherTourist::getOrderId, id);
                otherTouristWrapper.lambda().eq(SupplierOutsideOrderOtherTourist::getOtherId, other.getId());
                List<SupplierOutsideOrderOtherTourist> supplierOutsideOrderOtherTourists = supplierOutsideOrderOtherTouristMapper.selectList(otherTouristWrapper);
                if (CollectionUtils.isNotEmpty(supplierOutsideOrderOtherTourists)) {
                    List<SupplierOutsideOrderOtherTouristDTO> supplierOutsideOrderOtherTouristDTOList = EntityUtil.copyList(supplierOutsideOrderOtherTourists, SupplierOutsideOrderOtherTouristDTO.class);
                    other.setSupplierOutsideOrderOtherTouristDTOList(supplierOutsideOrderOtherTouristDTOList);
                }
            });
        }
        return supplierOutsideOrderOtherDTOS;
    }
}
